package com.dyzx.hbb.modules.sms.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.modules.sms.service.SmsService;
import com.dyzx.hbb.modules.sys.entity.SysUser;
import com.dyzx.hbb.modules.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/sms")
@RestController
@Tag(name = "16.短信管理", description = "短信接口")
public class SmsController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private SysUserService sysUserService;
    @Operation(summary = "发送验证码")
    @PostMapping("/vcode")
    public Result<Boolean> sendVerificationCode(@RequestParam(defaultValue = "") String phoneNumber) {
        if (phoneNumber.isEmpty()) {
            return Result.error("手机号不能为空");
        }
        if (!phoneNumber.matches("^1[3-9]\\d{9}$")) {
            return Result.error("手机号格式不正确");
        }
        SysUser byPhone = sysUserService.getByPhone(phoneNumber);
        if (byPhone == null) {
            return Result.error("该手机号未注册");
        }
        smsService.sendVerificationCode(phoneNumber);
        return Result.ok(true);
    }
}
